package com.zw.separate.aop;

import com.zw.separate.config.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @Description 数据源的切入面
 * @Author zw
 * @Date 2021/5/19 15:35
 * @Param
 * @Return
**/
@Aspect
@Component
@Slf4j
public class DataSourceAOP {

    /**
     * @Description 切换主库
     * @Author zw
     * @Date 2021/5/19 16:11
     * @Param []
     * @Return void
    **/
    @Before("(@annotation(com.zw.separate.annotation.Master) && !@annotation(com.zw.separate.annotation.Slave))")
    public void setWriteDataSourceType() {
        DynamicDataSource.master();
        log.info("写操作 dataSource切换到：Master");
    }

    /**
     * @Description 切换从库
     * @Author zw
     * @Date 2021/5/19 16:12
     * @Param []
     * @Return void
    **/
    @Before("(@annotation(com.zw.separate.annotation.Slave) && !@annotation(com.zw.separate.annotation.Master))")
    public void setReadDataSourceType() {
        DynamicDataSource.slave();
        log.info("读操作 dataSource切换到：Slave");
    }


}
